{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.examples.tutorials.mnist import input_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting D:\\CSDN\\week6\\tensorflow-without-a-phd-master\\train-images-idx3-ubyte.gz\n",
      "Extracting D:\\CSDN\\week6\\tensorflow-without-a-phd-master\\train-labels-idx1-ubyte.gz\n",
      "Extracting D:\\CSDN\\week6\\tensorflow-without-a-phd-master\\t10k-images-idx3-ubyte.gz\n",
      "Extracting D:\\CSDN\\week6\\tensorflow-without-a-phd-master\\t10k-labels-idx1-ubyte.gz\n"
     ]
    }
   ],
   "source": [
    "data_dir = r'D:\\CSDN\\week6\\tensorflow-without-a-phd-master'\n",
    "mnist = input_data.read_data_sets(data_dir,one_hot=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X = tf.placeholder(tf.float32, [None,784])\n",
    "Y_ = tf.placeholder(tf.float32,[None,10])\n",
    "\n",
    "W1 = tf.Variable(tf.truncated_normal([784,200],stddev=0.1))\n",
    "B1 = tf.Variable(tf.zeros([200]))\n",
    "\n",
    "W2 = tf.Variable(tf.truncated_normal([200,100],stddev=0.1))\n",
    "B2 = tf.Variable(tf.zeros([100]))\n",
    "\n",
    "W3 = tf.Variable(tf.truncated_normal([100,10],stddev=0.1))\n",
    "B3 = tf.Variable(tf.zeros([10]))\n",
    "\n",
    "Y1 = tf.nn.sigmoid(tf.matmul(X,W1)+B1)\n",
    "Y2 = tf.nn.sigmoid(tf.matmul(Y1,W2)+B2)\n",
    "Y = tf.matmul(Y2,W3)+B3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=Y_,logits=Y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "correct_prediction = tf.equal(tf.argmax(Y,1),tf.argmax(Y_,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_step = tf.train.GradientDescentOptimizer(1).minimize(cross_entropy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "init = tf.initialize_all_variables()\n",
    "sess = tf.Session()\n",
    "sess.run(init)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for i in range(40000):\n",
    "    batch_X,batch_Y = mnist.train.next_batch(100)\n",
    "    train_data = {X:batch_X,Y_:batch_Y}\n",
    "    sess.run(train_step,feed_dict = train_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9805\n"
     ]
    }
   ],
   "source": [
    "print(sess.run(accuracy,feed_dict={X:mnist.test.images,Y_:mnist.test.labels}))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
